package com.qiangqiang.resoldhouse.service;

import com.qiangqiang.resoldhouse.dto.AreaDTO;
import com.qiangqiang.resoldhouse.entity.Area;
import com.qiangqiang.resoldhouse.enums.AreaEnums;
import com.qiangqiang.resoldhouse.exception.AreaException;
import com.qiangqiang.resoldhouse.mapper.AreaMapper;
import com.qiangqiang.resoldhouse.util.TimeFormat;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : sky
 * @date : 2018/7/17
 * @description :
 */
@Service
public class AreaService extends BaseService {

    @Autowired
    private AreaMapper areaMapper;

    public AreaDTO findAreaById(Integer id) {
        Area area = areaMapper.findAreaById(id);
        if (area == null) {
            throw new AreaException(AreaEnums.AREA_NOT_EXIST);
        }
        AreaDTO areaDTO = new AreaDTO();
        BeanUtils.copyProperties(area, areaDTO);
        areaDTO.setCreateTime(TimeFormat.converTime(area.getCreateTime()));
        areaDTO.setUpdateTime(TimeFormat.converTime(area.getUpdateTime()));
        return areaDTO;
    }

    public List<AreaDTO> findAreaByName(String name) {
        List<Area> areas = areaMapper.findAreaByName(name);
        List<AreaDTO> results = new ArrayList<>();
        for(Area area : areas){
            AreaDTO areaDTO = new AreaDTO();
            BeanUtils.copyProperties(area, areaDTO);
            areaDTO.setUpdateTime(TimeFormat.converTime(area.getUpdateTime()));
            areaDTO.setCreateTime(TimeFormat.converTime(area.getCreateTime()));
            results.add(areaDTO);
        }
        return areaMapper.findAreaByName(name).stream().
                map(this::beanToDTO).collect(Collectors.toList());
    }

    public List<AreaDTO> findAllArea() {
//        List<Area> areas = areaMapper.findAllAreas();
//        List<AreaDTO> results = new ArrayList<>();
//        for(Area area : areas){
//            AreaDTO areaDTO = new AreaDTO();
//            BeanUtils.copyProperties(area, areaDTO);
//            areaDTO.setUpdateTime(TimeFormat.converTime(area.getUpdateTime()));
//            areaDTO.setCreateTime(TimeFormat.converTime(area.getCreateTime()));
//            results.add(areaDTO);
//        }
        return areaMapper.findAllAreas().stream().
                map(this::beanToDTO).collect(Collectors.toList());
    }

    public boolean deleteAreaById(Integer id) {
        Area area = areaMapper.findAreaById(id);
        if (area == null) {
            throw new AreaException(AreaEnums.AREA_NOT_EXIST);
        }
        return areaMapper.deleteAreaById(id) > 0;
    }

    public boolean insertArea(Area area) {
        area.setCreateTime(Instant.now().toEpochMilli());
        area.setUpdateTime(Instant.now().toEpochMilli());
        return areaMapper.insertArea(area) > 0;
    }

    public boolean updateArea(Area area) {
        Area oldArea = areaMapper.findAreaById(area.getAreaId());
        area.setUpdateTime(Instant.now().toEpochMilli());
        area.setCreateTime(oldArea.getCreateTime());
        return areaMapper.updateArea(area) > 0;
    }
}
